/**
 * Copyright (c) 2008, Andrew Carter
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, are 
 * permitted provided that the following conditions are met:
 * 
 * Redistributions of source code must retain the above copyright notice, this list of 
 * conditions and the following disclaimer. Redistributions in binary form must reproduce 
 * the above copyright notice, this list of conditions and the following disclaimer in 
 * the documentation and/or other materials provided with the distribution. Neither the 
 * name of Andrew Carter nor the names of contributors may be used to endorse or promote 
 * products derived from this software without specific prior written permission. 
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
 * SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 
 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.acarter.scenemonitor.propertydescriptor.definitions;

import java.util.concurrent.Callable;

import com.acarter.propertytable.Property;
import com.acarter.propertytable.PropertySection;
import com.acarter.propertytable.PropertySectionState;
import com.acarter.propertytable.PropertyTable;
import com.acarter.propertytable.propertyobject.BooleanPropertyObject;
import com.acarter.propertytable.propertyobject.EnumPropertyObject;
import com.acarter.propertytable.propertyobject.IntegerPropertyObject;
import com.acarter.propertytable.propertyobject.StringPropertyObject;
import com.acarter.propertytable.propertyobject.BooleanPropertyObject.I_BooleanPropertyObjectListener;
import com.acarter.propertytable.propertyobject.EnumPropertyObject.I_EnumPropertyObjectListener;
import com.acarter.propertytable.propertyobject.IntegerPropertyObject.I_IntegerPropertyObjectListener;
import com.acarter.scenemonitor.propertydescriptor.A_PropertyPage;
import com.acarter.scenemonitor.propertydescriptor.propertyobject.QuaternionPropertyObject;
import com.acarter.scenemonitor.propertydescriptor.propertyobject.Vector3fPropertyObject;
import com.acarter.scenemonitor.propertydescriptor.propertyobject.QuaternionPropertyObject.I_QuaternionPropertyObjectListener;
import com.acarter.scenemonitor.propertydescriptor.propertyobject.Vector3fPropertyObject.I_Vector3fPropertyObjectListener;
import com.jme.bounding.BoundingBox;
import com.jme.bounding.BoundingCapsule;
import com.jme.bounding.BoundingSphere;
import com.jme.bounding.BoundingVolume;
import com.jme.bounding.OrientedBoundingBox;
import com.jme.math.Quaternion;
import com.jme.math.Vector3f;
import com.jme.scene.Spatial;
import com.jme.scene.Spatial.CullHint;
import com.jme.scene.Spatial.LightCombineMode;
import com.jme.scene.Spatial.NormalsMode;
import com.jme.scene.Spatial.TextureCombineMode;
import com.jme.util.GameTaskQueueManager;

/**
 * @author Carter
 *
 */
public class SpatialPropertyPage extends A_PropertyPage {
	
	/**  */
	private Spatial spatial = null;
	
	/**
	 * 
	 */
	public SpatialPropertyPage() {
		
		PropertySection section = new PropertySection("Spatial");
		section.addProperty(new Property("Name", new StringPropertyObject()));
		section.addProperty(new Property("Translation", new Vector3fPropertyObject()));
		section.addProperty(new Property("Rotation", new QuaternionPropertyObject()));
		section.addProperty(new Property("Scale", new Vector3fPropertyObject()));
		section.addProperty(new Property("Bounding Volume", new EnumPropertyObject<E_BoundingVolume>()));
		section.addProperty(new Property("Cull Hint", new EnumPropertyObject<CullHint>()));
		section.addProperty(new Property("Light Combine Mode", new EnumPropertyObject<LightCombineMode>()));
		section.addProperty(new Property("Texture Combine Mode", new EnumPropertyObject<TextureCombineMode>()));
		section.addProperty(new Property("Normals Mode", new EnumPropertyObject<NormalsMode>()));
		section.addProperty(new Property("Collidable", new BooleanPropertyObject()));
		section.addProperty(new Property("Z Order", new IntegerPropertyObject()));
		section.addProperty(new Property("RenderQMode", new EnumPropertyObject<E_RenderQueueMode>()));
		
		section.setState(PropertySectionState.EXPANDED);
		
		model.addPropertySection(section);
		
		section = new PropertySection("Spatial Locks");
		section.addProperty(new Property("Lock Bounds", new BooleanPropertyObject()));
		section.addProperty(new Property("Lock Meshes", new BooleanPropertyObject()));
		section.addProperty(new Property("Lock Shadows", new BooleanPropertyObject()));
		section.addProperty(new Property("Lock Transforms", new BooleanPropertyObject()));
		section.addProperty(new Property("Lock Branch", new BooleanPropertyObject()));
		
		section.setState(PropertySectionState.EXPANDED);
		
		model.addPropertySection(section);
	}
	
	/**
	 * @see com.acarter.scenemonitor.propertydescriptor.A_PropertyPage#updateInfo(com.acarter.propertytable.PropertyTableModel, java.lang.Object)
	 */
	@Override
	public void updateModel(PropertyTable table, Object object) {

		if(object instanceof Spatial) {
			
			Spatial spatial = (Spatial)object;
			
			if(this.spatial == null || !this.spatial.equals(spatial)) {
				
				this.spatial = spatial;
				updateListeners(spatial);
				
				spatial.updateRenderState();
				spatial.updateGeometricState(0, true);
			}
		}
	}
	
	/**
	 * 
	 * @param table
	 * @param object
	 */
	@SuppressWarnings("unchecked")
	protected void updateListeners(final Spatial spatial) {
		
		StringPropertyObject	lcl_name = (StringPropertyObject)model.getPropertySection("Spatial").getProperty("Name").getPropertyObject();
		
		lcl_name.SetListener(new StringPropertyObject.I_StringPropertyObjectListener() {
			public String readValue() {
				return spatial.getName();
			}

			public void saveValue(String value) {
				
				spatial.setName(value);
			}
		});
		
		Vector3fPropertyObject translation = (Vector3fPropertyObject)model.getPropertySection("Spatial").getProperty("Translation").getPropertyObject();
		
		translation.SetListener(new I_Vector3fPropertyObjectListener() {

			public Vector3f readValue() {
				
				return spatial.getLocalTranslation();
			}

			public void saveValue(Vector3f value) {
				
				spatial.setLocalTranslation(value);
			}
		});
		
		QuaternionPropertyObject rotation = (QuaternionPropertyObject)model.getPropertySection("Spatial").getProperty("Rotation").getPropertyObject();
		
		rotation.SetListener(new I_QuaternionPropertyObjectListener() {

			public Quaternion readValue() {
				
				return spatial.getLocalRotation();
			}

			public void saveValue(Quaternion value) {
				
				spatial.setLocalRotation(value);
			}
		});
		
		Vector3fPropertyObject scale = (Vector3fPropertyObject)model.getPropertySection("Spatial").getProperty("Scale").getPropertyObject();
		
		scale.SetListener(new I_Vector3fPropertyObjectListener() {

			public Vector3f readValue() {
				
				return spatial.getLocalScale();
			}

			public void saveValue(Vector3f value) {
				
				spatial.setLocalScale(value);
			}
		});
		
		EnumPropertyObject<E_BoundingVolume> bounding = (EnumPropertyObject<E_BoundingVolume>)model.getPropertySection("Spatial").getProperty("Bounding Volume").getPropertyObject();
		
		bounding.SetListener(new I_EnumPropertyObjectListener<E_BoundingVolume>() {

			public E_BoundingVolume readValue() {
				
				return E_BoundingVolume.getConstant(spatial.getWorldBound());
			}

			public void saveValue(E_BoundingVolume value) {
				
				BoundingVolume bv = null;
				
				if(value == E_BoundingVolume.BOX)
					bv = new BoundingBox();
				else if(value == E_BoundingVolume.CAPSULE)
					bv = new BoundingCapsule();
				else if(value == E_BoundingVolume.SPHERE)
					bv = new BoundingSphere();
				else if(value == E_BoundingVolume.ORIENTEDBOX)
					bv = new OrientedBoundingBox();
				
				spatial.setModelBound(bv);
				spatial.updateModelBound();
			}
		});
		
		EnumPropertyObject<CullHint> cullHint = (EnumPropertyObject<CullHint>)model.getPropertySection("Spatial").getProperty("Cull Hint").getPropertyObject();
		
		cullHint.SetListener(new I_EnumPropertyObjectListener<CullHint>() {
			public CullHint readValue() {
				
				return spatial.getCullHint();
			}

			public void saveValue(CullHint value) {
				
				spatial.setCullHint(value);
			}
		});
		
		EnumPropertyObject<LightCombineMode> lightComb = (EnumPropertyObject<LightCombineMode>)model.getPropertySection("Spatial").getProperty("Light Combine Mode").getPropertyObject();
		
		lightComb.SetListener(new I_EnumPropertyObjectListener<LightCombineMode>() {

			public LightCombineMode readValue() {
				
				return spatial.getLightCombineMode();
			}

			public void saveValue(LightCombineMode value) {
				
				spatial.setLightCombineMode(value);
			}
		});
		
		EnumPropertyObject<TextureCombineMode> textureComb = (EnumPropertyObject<TextureCombineMode>)model.getPropertySection("Spatial").getProperty("Texture Combine Mode").getPropertyObject();
		
		textureComb.SetListener(new I_EnumPropertyObjectListener<TextureCombineMode>() {

			public TextureCombineMode readValue() {
				
				return spatial.getTextureCombineMode();
			}

			public void saveValue(TextureCombineMode value) {
				
				spatial.setTextureCombineMode(value);
			}
		});
		
		EnumPropertyObject<NormalsMode> normalsMode = (EnumPropertyObject<NormalsMode>)model.getPropertySection("Spatial").getProperty("Normals Mode").getPropertyObject();
		
		normalsMode.SetListener(new I_EnumPropertyObjectListener<NormalsMode>() {

			public NormalsMode readValue() {
				
				return spatial.getNormalsMode();
			}

			public void saveValue(NormalsMode value) {
				
				spatial.setNormalsMode(value);
			}
		});
		
		BooleanPropertyObject collidable = (BooleanPropertyObject)model.getPropertySection("Spatial").getProperty("Collidable").getPropertyObject();
		
		collidable.SetListener(new I_BooleanPropertyObjectListener() {

			public boolean readValue() {
				
				return spatial.isCollidable();
			}

			public void saveValue(boolean value) {
				
				spatial.setIsCollidable(value);
			}
		});
		
		IntegerPropertyObject zOrder = (IntegerPropertyObject)model.getPropertySection("Spatial").getProperty("Z Order").getPropertyObject();
		
		zOrder.SetListener(new I_IntegerPropertyObjectListener() {

			public int readValue() {
				
				return spatial.getZOrder();
			}

			public void saveValue(int value) {
				
				spatial.setZOrder(value);
			}
		});
		
		EnumPropertyObject<E_RenderQueueMode> rqMode = (EnumPropertyObject<E_RenderQueueMode>)model.getPropertySection("Spatial").getProperty("RenderQMode").getPropertyObject();
		
		rqMode.SetListener(new I_EnumPropertyObjectListener<E_RenderQueueMode>() {
			public E_RenderQueueMode readValue() {
				
				return E_RenderQueueMode.getConstant(spatial.getRenderQueueMode());
			}

			public void saveValue(E_RenderQueueMode value) {
				spatial.setRenderQueueMode(value.getType());
			}
		});
		
		BooleanPropertyObject lockBounds = (BooleanPropertyObject)model.getPropertySection("Spatial Locks").getProperty("Lock Bounds").getPropertyObject();
		
		lockBounds.SetListener(new I_BooleanPropertyObjectListener() {

			public boolean readValue() {
				
				return (spatial.getLocks() & Spatial.LOCKED_BOUNDS) != 0;
			}

			public void saveValue(boolean value) {
				
				if(value)
					spatial.lockBounds();
				else
					spatial.unlockBounds();
			}
		});
		
		BooleanPropertyObject lockMeshes = (BooleanPropertyObject)model.getPropertySection("Spatial Locks").getProperty("Lock Meshes").getPropertyObject();
		
		lockMeshes.SetListener(new I_BooleanPropertyObjectListener() {

			public boolean readValue() {
				
				return (spatial.getLocks() & Spatial.LOCKED_MESH_DATA) != 0;
			}

			public void saveValue(boolean value) {
				
				if(value) {
					GameTaskQueueManager.getManager().update(new Callable<Void>() {
						public Void call() throws Exception {
							spatial.lockMeshes();
							return null;
						}
						
					});
				}	
				else {
					GameTaskQueueManager.getManager().update(new Callable<Void>() {
						public Void call() throws Exception {
							spatial.unlockMeshes();
							return null;
						}
						
					});
				}	
			}
		});
		
		BooleanPropertyObject lockShadows = (BooleanPropertyObject)model.getPropertySection("Spatial Locks").getProperty("Lock Shadows").getPropertyObject();
		
		lockShadows.SetListener(new I_BooleanPropertyObjectListener() {

			public boolean readValue() {
				
				return (spatial.getLocks() & Spatial.LOCKED_SHADOWS) != 0;
			}

			public void saveValue(boolean value) {
				
				if(value)
					spatial.lockShadows();
				else
					spatial.unlockShadows();
			}
		});
		
		BooleanPropertyObject lockTransforms = (BooleanPropertyObject)model.getPropertySection("Spatial Locks").getProperty("Lock Transforms").getPropertyObject();
		
		lockTransforms.SetListener(new I_BooleanPropertyObjectListener() {

			public boolean readValue() {
				
				return (spatial.getLocks() & Spatial.LOCKED_TRANSFORMS) != 0;
			}

			public void saveValue(boolean value) {
				
				if(value)
					spatial.lockTransforms();
				else
					spatial.unlockTransforms();
			}
		});
		
		BooleanPropertyObject lockBranch = (BooleanPropertyObject)model.getPropertySection("Spatial Locks").getProperty("Lock Branch").getPropertyObject();
		
		lockBranch.SetListener(new I_BooleanPropertyObjectListener() {

			public boolean readValue() {
				
				return (spatial.getLocks() & Spatial.LOCKED_BRANCH) != 0;
			}

			public void saveValue(boolean value) {
				
				if(value)
					spatial.lockBranch();
				else
					spatial.unlockBranch();
			}
		});
	}

	/**
	 * Enumerates the permissible render queue mode.
	 * 
	 * @author Andrew Carter
	 */
	public enum E_RenderQueueMode {

	    /** The Spatial will inherit its render queue state from its parent. */
	    QUEUE_INHERIT ("QUEUE_INHERIT", 0),

	    /** The Spatial will skip render queueing. */
	    QUEUE_SKIP("QUEUE_SKIP", 1),

	    /** The Spatial will render in the opaque bucket. */
	    QUEUE_OPAQUE("QUEUE_OPAQUE", 2),

	    /** The Spatial will render in the transparent bucket. */
	    QUEUE_TRANSPARENT("QUEUE_TRANSPARENT", 3),

	    /** The Spatial will render in the ortho bucket. */
	    QUEUE_ORTHO("QUEUE_ORTHO", 4);

		/** String representation of the enum value */
		private String name;

		/** Integer value of the enum value */
		private int value;

		/**
		 * Private constructor.
		 * 
		 * @param value
		 *            String literal of the enum
		 */
		private E_RenderQueueMode(String name, int value) {

			this.name = name;
			this.value = value;
		}

		/**
		 * Returns the enum constant matching the name string.
		 * 
		 * @param name
		 *            string value of enum constant
		 * @return matching enum constant or null
		 */
		public static E_RenderQueueMode getConstant(String name) {

			for (E_RenderQueueMode constant : E_RenderQueueMode.values()) {

				if (constant.toString().compareToIgnoreCase(name) == 0)
					return constant;
			}

			return null;
		}

		/**
		 * Returns the enum constant matching the type value.
		 * 
		 * @param type
		 *            type value of enum constant
		 * @return matching enum constant or null
		 */
		public static E_RenderQueueMode getConstant(int type) {

			for (E_RenderQueueMode constant : E_RenderQueueMode.values()) {

				if (constant.getType() == type)
					return constant;
			}

			return null;
		}

		/**
		 * Returns the string value of the constant.
		 */
		public String toString() {

			return name;
		}

		/**
		 * Returns the integer value of the constant.
		 * 
		 * @return
		 */
		public int getType() {

			return value;
		}
	}
	
	/**
	 * Enumerates the permissible bounding volume types.
	 * 
	 * @author Andrew Carter
	 */
	protected enum E_BoundingVolume {

		NONE("None", ""),

		BOX("Bounding Box", "BoundingBox"),

		CAPSULE("Bounding Capsule", "BoundingCapsule"),

		SPHERE("Bounding Sphere", "BoundingSphere"),

		ORIENTEDBOX("Oriented Box", "OrientedBoundingBox");

		/** String representation of the enum value */
		private String name;

		/** Integer value of the enum value */
		private String value;

		/**
		 * Private constructor.
		 * 
		 * @param value
		 *            String literal of the enum
		 */
		private E_BoundingVolume(String name, String value) {

			this.name = name;
			this.value = value;
		}

		/**
		 * Returns the enum constant matching the name string.
		 * 
		 * @param name
		 *            string value of enum constant
		 * @return matching enum constant or null
		 */
		public static E_BoundingVolume getConstant(String name) {

			for (E_BoundingVolume constant : E_BoundingVolume.values()) {

				if (constant.toString().compareToIgnoreCase(name) == 0)
					return constant;
			}

			return null;
		}

		/**
		 * Returns the enum constant matching the type value.
		 * 
		 * @param type
		 *            type value of enum constant
		 * @return matching enum constant or null
		 */
		public static E_BoundingVolume getConstant(BoundingVolume type) {

			if (type != null) {
				
				for (E_BoundingVolume constant : E_BoundingVolume.values()) {

					if (constant.getType().compareToIgnoreCase(type.getClass().getSimpleName()) == 0)
						return constant;
				}
			}
			
			return NONE;
		}

		/**
		 * Returns the string value of the constant.
		 */
		public String toString() {

			return name;
		}

		/**
		 * Returns the integer value of the constant.
		 * @return 
		 */
		public String getType() {

			return value;
		}
	}
}
